﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Infrastructure;
using PickingBusiness;
using Picking.Common;
using System.IO;
using Infrastructure.Util;

namespace Picking_Winform.Environment
{
    public partial class SebangoAndProductBookView : FormBase
    {
        private SebangoAndProductBookModel model = new SebangoAndProductBookModel();
        private bool hasDeletedData = false;

        public SebangoAndProductBookView()
        {
            InitializeComponent();
            this.Controls.Add(base.txtTitle);
            this.comboBox1.Items.Clear();
            this.comboBox1.Items.AddRange(base.items);
            this.comboBox1.SelectedIndex = 0;
        }

        /// <summary>
        /// Set Form Title
        /// </summary>
        private void SetFormTitle()
        {
            this.Text = txtTitle.Text = "背番号和生产指示书的设定";
        }


        private void SebangoAndProductBookView_Shown(object sender, EventArgs e)
        {
            InitSebangoAndProductBook();
            //this.SetFormTitle();
        }

        private void SebangoAndProductBookView_Load(object sender, EventArgs e)
        {
            this.hasDeletedData = false;
        }

        /// <summary>
        /// Do Validation
        /// </summary>
        /// <returns></returns>
        private bool DoValidation()
        {
            List<int> lightNumbers = new List<int>();
            List<string> designNoList = new List<string>();

            if (string.IsNullOrEmpty(this.txtSebango.Text.Trim())
                && string.IsNullOrEmpty(this.txtMachineName.Text.Trim()))
            {
                if (this.hasDeletedData)
                {
                    return true;
                }
                else
                {
                    MessageBox.Show("背番号和生产指示书均不能为空!",
                                    "验证",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);
                    return false;
                }
            }

            int intSebango = 0;

            string sebango = this.txtSebango.Text.Trim();

            if (string.IsNullOrEmpty(sebango))
            {
                MessageBox.Show("背番号不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }
            else
            {
                if (!int.TryParse(sebango, out intSebango))
                {
                    MessageBox.Show("背番号必须是个整型数值!",
                                    "验证",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);

                    return false;
                }

                if (intSebango < 0)
                {
                    MessageBox.Show("背番号不能为负数！",
                                    "验证",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);

                    return false;
                }

                if (!this.txtSebango.ReadOnly)
                {
                    foreach (DataRow row in this.model.SebangoAndProductBookTable.Rows)
                    {
                        if (string.Equals(this.txtSebango.Text.Trim(), row["Sebango_ID"].ToString().Trim()))
                        {
                            MessageBox.Show("背番号（" + this.txtSebango.Text.Trim() + "）不能重复!",
                                            "验证",
                                            MessageBoxButtons.OK,
                                            MessageBoxIcon.Warning);
                            return false;
                        }
                    }
                }
            }


            string machineName = this.txtMachineName.Text.Trim();

            if (string.IsNullOrEmpty(machineName))
            {

                MessageBox.Show("生产指示书不能为空!",
                                "验证",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Warning);
                return false;
            }

            return true;
        }

        /// <summary>
        /// Save Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            this.btnSave.Focus();

            if (!this.DoValidation())
            {
                return;
            }

            if (!this.txtSebango.ReadOnly)
            {
                if (!string.IsNullOrEmpty(this.txtSebango.Text.Trim())
                    && !string.IsNullOrEmpty(this.txtMachineName.Text.Trim()))
                {
                    DataRow newRow = this.model.SebangoAndProductBookTable.NewRow();

                    newRow["Sebango_ID"] = this.txtSebango.Text.Trim();
                    newRow["ProductBook_Name"] = this.txtMachineName.Text.Trim();
                    this.model.SebangoAndProductBookTable.Rows.Add(newRow);
                }
            }
            else
            {
                foreach (DataRow row in this.model.SebangoAndProductBookTable.Rows)
                {
                    if (string.Equals(this.txtSebango.Text.Trim(), row["Sebango_ID"].ToString().Trim()))
                    {
                        row["ProductBook_Name"] = this.txtMachineName.Text.Trim();
                    }
                }
            }

            string lineCode = this.GetLineCode();
            bool result = Configuration.SaveSebangoAndProductBook(this.model, lineCode);

            if (result)
            {
                this.txtSebango.ReadOnly = false;
                this.txtSebango.BackColor = Color.White;

                MessageBox.Show("保存成功!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.hasDeletedData = false;
            }
            else
            {
                MessageBox.Show("保存失败!", "保存", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Cancel Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.btnCancel.Focus();
            this.InitSebangoAndProductBook();
            this.txtSebango.Text = string.Empty;
            this.txtMachineName.Text = string.Empty;
            this.txtSebango.ReadOnly = false;
            this.txtSebango.BackColor = Color.White;
            this.hasDeletedData = false;
        }

        /// <summary>
        /// Return Event
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReturn_Click(object sender, EventArgs e)
        {
            this.CloseCurrentWindow();
        }

        private void grdMachineSet_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex < 0)
            {
                return;
            }

            if (e.RowIndex < 0)
            {
                return;
            }

            if ((this.grdMachineSet.NewRowIndex < 0)
                || (e.RowIndex < this.grdMachineSet.NewRowIndex))
            {
                string sebango
                    = this.model.SebangoAndProductBookTable.Rows[e.RowIndex]["Sebango_ID"].ToString().Trim();

                string machineName
                    = this.model.SebangoAndProductBookTable.Rows[e.RowIndex]["ProductBook_Name"].ToString().Trim();

                if (string.Equals(this.grdMachineSet.Columns[e.ColumnIndex].Name, "colDelete"))
                {
                    this.grdMachineSet.Rows.RemoveAt(e.RowIndex);
                    this.grdMachineSet.Update();
                    this.grdMachineSet.EndEdit();
                    this.hasDeletedData = true;
                }
                else
                {
                    this.txtSebango.Text = sebango;
                    this.txtMachineName.Text = machineName;

                    this.txtSebango.ReadOnly = true;
                    this.txtSebango.BackColor = Color.LightGray;
                }
            }
        }

        /// <summary>
        /// Clear Sebango And Machine Name
        /// </summary>
        private void ClearSebangoAndMachineName()
        {
            this.txtSebango.Text = string.Empty;
            this.txtMachineName.Text = string.Empty;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.InitSebangoAndProductBook();
            this.ClearSebangoAndProductBook();
            this.txtSebango.ReadOnly = false;
            this.txtSebango.BackColor = Color.White;
            this.hasDeletedData = false;
        }

        private string GetLineCode()
        {
            string lineCode = ConfigInfo.LineCodeA;

            if (this.comboBox1.SelectedIndex == 0)
            {
                lineCode = ConfigInfo.LineCodeA;
            }
            else if (this.comboBox1.SelectedIndex == 1)
            {
                lineCode = ConfigInfo.LineCodeB;
            }
            else if (this.comboBox1.SelectedIndex == 2)
            {
                lineCode = ConfigInfo.LineCodeC;
            }

            return lineCode;
        }

        /// <summary>
        /// Init Sebango And ProductBook
        /// Get Sebango And ProductBook From Database
        /// </summary>
        private void InitSebangoAndProductBook()
        {
            string lineCode = this.GetLineCode();
            this.model = Configuration.GetSebangoAndProductBook(lineCode);

            this.grdMachineSet.DataSource = this.model.SebangoAndProductBookTable;

            this.grdMachineSet.ClearSelection();
        }

        /// <summary>
        /// Clear Sebango And ProductBook
        /// </summary>
        private void ClearSebangoAndProductBook()
        {
            this.txtSebango.Text = string.Empty;
            this.txtMachineName.Text = string.Empty;
        }

    }
}
